package com.api.syh56.shengyunhui.service.impl;

import com.api.syh56.shengyunhui.entity.DedicatedLines;
import com.api.syh56.shengyunhui.entity.dto.DedicatedLinesDto;
import com.api.syh56.shengyunhui.exception.BaseException;
import com.api.syh56.shengyunhui.mapper.DedicatedLinesMapper;
import com.api.syh56.shengyunhui.service.ApiAreaDbService;
import com.api.syh56.shengyunhui.service.DedicatedLinesService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DedicatedLinesServiceImpl extends ServiceImpl<DedicatedLinesMapper,DedicatedLines> implements DedicatedLinesService {

    @Autowired
    private DedicatedLinesMapper dMapper;

    @Autowired
    private ApiAreaDbService apiAreaDbService;

    @Override
    public void insert(DedicatedLinesDto dedicatedLinesDto) {
        DedicatedLines dedicatedLines = DtoToDedicatedLines(dedicatedLinesDto);
        int insert = dMapper.insert(dedicatedLines);
        if (insert != 1) {
            throw new BaseException("插入专线失败");
        }
    }

    @Override
    public void deleteBy(DedicatedLines dedicatedLines) {
        DedicatedLines lines = dMapper.selectById(dedicatedLines.getId());
        if (lines == null) {
            throw new BaseException("id不存在");
        }
        int delete = dMapper.deleteById(dedicatedLines);
        if (delete != 1) {
            throw new BaseException("删除专线失败");
        }
    }

    @Override
    public void updateBy(DedicatedLinesDto dedicatedLinesDto) {
        if (dedicatedLinesDto.getId() == null) {
            throw new BaseException("请输入id");
        }
        DedicatedLines lines = dMapper.selectById(dedicatedLinesDto.getId());
        if (lines == null){
            throw new BaseException("id不存在，请输入正确id");
        }
        DedicatedLines dedicatedLines = DtoToDedicatedLines(dedicatedLinesDto);
        int updateById = dMapper.updateById(dedicatedLines);
        if (updateById != 1 ){
            throw new BaseException("修改参数失败");
        }
    }

    private DedicatedLines DtoToDedicatedLines(DedicatedLinesDto dedicatedLinesDto) {
        DedicatedLines dedicatedLines = new DedicatedLines();
        ArrayList<String> oriStrings = apiAreaDbService.selectByCity(dedicatedLinesDto.getOriginPcd());
        dedicatedLinesDto.setOriginProvince(oriStrings.get(0));
        dedicatedLinesDto.setOriginCity(oriStrings.get(1));
        dedicatedLinesDto.setOriginDistrict(oriStrings.get(2));
        ArrayList<String> purStrings = apiAreaDbService.selectByCity(dedicatedLinesDto.getPurPcd());
        dedicatedLinesDto.setPurProvince(purStrings.get(0));
        dedicatedLinesDto.setPurCity(purStrings.get(1));
        dedicatedLinesDto.setPurDistrict(purStrings.get(2));
        BeanUtils.copyProperties(dedicatedLinesDto,dedicatedLines);
        return dedicatedLines;
    }


    @Override
    public List<DedicatedLines> selectAll() {
        List<DedicatedLines> dLinesList = dMapper.selectList(null);
        if (dLinesList.isEmpty()) {
            throw new BaseException("查询失败");
        }
        return dLinesList;
    }

    @Override
    public DedicatedLines selectBy(DedicatedLines dedicatedLines) {
        QueryWrapper<DedicatedLines> wrapper = new QueryWrapper();
        wrapper.eq("id",dedicatedLines.getId());
        if (wrapper == null) {
            throw new BaseException("id不存在");
        }
        DedicatedLines dLines = dMapper.selectById(wrapper);
        if (dLines == null) {
            throw new BaseException("查询失败");
        }
        return dLines;
    }

    @Override
    public Page<DedicatedLines> selectByCity(String originCity, String purCity, Integer pageSize, Integer pageNum) {
        System.out.println(originCity+"---"+purCity);
        QueryWrapper<DedicatedLines> wrapper = new QueryWrapper();
        wrapper.lambda().eq(DedicatedLines::getOriginCity,originCity);
        wrapper.lambda().eq(DedicatedLines::getPurCity,purCity);
        System.out.println("sPageSize:"+pageSize);
        Page<DedicatedLines> page = new Page<>(pageNum,pageSize);
        Page<DedicatedLines> dedicatedLinesList = dMapper.selectPage(page, wrapper);
        if (dedicatedLinesList == null) {
                throw new BaseException("查看失败");
            }
        return dedicatedLinesList;
    }
}
